/********************************************************************
 * PROJECT: CSAIR ROST PROJECT.
 * SUPPLIER: FOREVER OPEN SOURCE SOFTWARE INC.
 ********************************************************************
 * FILE: SelectableTree.java
 * (C) Copyright Foliage Team 2013, All Rights Reserved.
 ********************************************************************/
package org.foliage.rcp.ui.custom;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

public class SelectableTree extends Composite {

	// ----------------------------------------------- 

	private Text text;

	private Tree tree;

	private Shell shell;

	private List list;

	// ----------------------------------------------- 

	public SelectableTree(Composite parent, int style) {
		super(parent, style);
		init();
		initListeners();
	}

	// ----------------------------------------------- 

	private void init() {
		setLayout(new GridLayout(1, false));

		text = new Text(this, SWT.BORDER);
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		text.setCapture(false);

		tree = new Tree(this, SWT.BORDER);
		tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		createDropDownList();
	}

	// ----------------------------------------------- 

	private void initListeners() {
		text.addListener(SWT.Modify, new Listener() {

			@Override
			public void handleEvent(Event event) {
				if (!text.getText().isEmpty()) {
					list.removeAll();
					int size = tree.getColumnCount();
					TreeItem[] items = tree.getItems();
					for (int i = 0; i < items.length; i++) {
						String itemText = items[i].getText(0);
						for (int j = 1; j < size; j++) {
							itemText += " | " + items[i].getText(j);
						}
						if (itemText.contains(text.getText())) {
							list.add(itemText);
							list.setData(itemText, i);
						}
					}
					if (list.getItemCount() > 0) {
						Point location = new Point(text.getBounds().x, text.getBounds().y + text.getBounds().height);
						location = Display.getDefault().map(SelectableTree.this, null, location);
						shell.setLocation(location);
						shell.setSize(text.getBounds().width, 100);
						shell.layout();
						shell.setVisible(true);
					} else {
						shell.setVisible(false);
					}
				} else {
					shell.setVisible(false);
				}
			}
		});
	}

	private void createDropDownList() {
		shell = new Shell(Display.getDefault(), SWT.ON_TOP | SWT.TOOL | SWT.NO_TRIM | SWT.NO_FOCUS);
		shell.setSize(100, 100);
		shell.setLayout(new FillLayout());
		list = new List(shell, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);
		list.addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event event) {
				text.setText(list.getSelection()[0]);
				int index = (Integer) list.getData(text.getText());
				tree.select(tree.getItem(index));
				tree.setTopItem(tree.getItem(index));
				shell.setVisible(false);
			}

		});
	}

	// ----------------------------------------------- 

	public Tree getTree() {
		return tree;
	}

}
